var KOC = require("koc.common.utils");
var Config_Admin = require('../../config.admins.js');
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");

module.exports = {
    Main: function (req, res, callback) {
        res.render("system-manage/main", callback);
    },
    //系统管理
    Manager_Manage: {
        /////////////////////////////
        //管理员列表
        ////////////////////////////
        List: function (req, res, callback) {
            res.render("system-manage/manager-manage/list", callback);
        },
        /////////////////////////////
        //添加管理员
        ////////////////////////////
        Add: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                IsMaster: false,
                IsKeeper: false,
                IsEngineer: false,
                User_ID: KOC.StringUtils.ToInt(req.params.id, -1),
                RealName: "",
                Phone: "",
                Position: "",
                Remark: "",
                Store_List: "",
                Job_Number: "",
                Group_ID: -1,
                Status: -1,
                RightCode: {
                    TreeData: [],
                    Data: [],
                    Default: [],
                    All: []
                },
                MerchantGroup_List: []
            };
            //权限
            var _RightCode = "";
            KOC.Async.series([
                //读取员工信息
                function (callback) {
                    if (_PageData.User_ID <= 0) {
                        callback(null);
                        return;
                    }
                    DALFactory.User.User_Admin.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "无此管理员";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_ID = KOC.StringUtils.ToJSONStr(retValue.returnObject.User_ID);
                        _PageData.RealName = KOC.StringUtils.ToJSONStr(retValue.returnObject.RealName);
                        _PageData.Phone = KOC.StringUtils.ToJSONStr(retValue.returnObject.Phone);
                        _PageData.IsMaster = retValue.returnObject.IsMaster == 1;
                        //_PageData.IsKeeper = retValue.returnObject.IsKeeper == 1;
                        //_PageData.IsEngineer = retValue.returnObject.IsEngineer == 1;
                        //_PageData.Position = KOC.StringUtils.ToString(retValue.returnObject.Position);
                        //_PageData.Remark = KOC.StringUtils.ToString(retValue.returnObject.Remark);
                        //_PageData.Store_List = retValue.returnObject.Store_List;
                        _PageData.Status = retValue.returnObject.Status;
                        _RightCode = retValue.returnObject.RightCode;

                        callback(null);
                    });
                },
                //读取权限列表
                function (callback) {
                    KOC.Lodash.forEach(Config_Admin.MenuConfig, function (ThisValue_I) {
                        if (ThisValue_I.Setup === false) {
                            return true;
                        }
                        var _Item_II = [];
                        KOC.Lodash.forEach(ThisValue_I.Child, function (ThisValue_II) {
                            if (ThisValue_II.Setup === false) {
                                return true;
                            }
                            var _Item_III = [];
                            KOC.Lodash.forEach(ThisValue_II.Child, function (ThisValue_III) {
                                if (ThisValue_III.Setup === false) {
                                    return true;
                                }
                                _Item_III.push({
                                    Text: ThisValue_III.Text,
                                    Code: ThisValue_III.SetupCode || ThisValue_III.RightCode,
                                    Explain: ThisValue_III.Explain
                                });
                                if (_RightCode.indexOf(ThisValue_III.RightCode) >= 0) {
                                    _PageData.RightCode.Data.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                                }
                                _PageData.RightCode.All.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                            });
                            _Item_II.push({
                                Text: ThisValue_II.Text,
                                Code: ThisValue_II.RightCode,
                                Explain: ThisValue_II.Explain,
                                Checked: _RightCode.indexOf(ThisValue_II.RightCode) >= 0,
                                List: _Item_III
                            });
                        });
                        _PageData.RightCode.TreeData.push({
                            Text: ThisValue_I.Text,
                            Code: ThisValue_I.RightCode,
                            Explain: ThisValue_I.Explain,
                            Checked: _RightCode.indexOf(ThisValue_I.RightCode) >= 0,
                            List: _Item_II
                        });
                    });
                    _PageData.RightCode.Default = _PageData.RightCode.Data;
                    callback(null);
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render('system-manage/manager-manage/add', callback);
            });
        },
        /////////////////////////////
        //修改管理员
        ////////////////////////////
        Edit: function (req, res, callback) {
            var _PageData = {
                flag: 0,
                IsMaster: false,
                IsKeeper: false,
                IsEngineer: false,
                User_ID: KOC.StringUtils.ToInt(req.params.id, -1),
                RealName: "",
                Phone: "",
                Position: "",
                Remark: "",
                Store_List: "",
                Job_Number: "",
                Group_ID: -1,
                Status: -1,
                RightCode: {
                    TreeData: [],
                    Data: [],
                    Default: [],
                    All: []
                },
                MerchantGroup_List: []
            };
            //权限
            var _RightCode = "";
            KOC.Async.series([
                //读取员工信息
                function (callback) {
                    if (_PageData.User_ID <= 0) {
                        callback(null);
                        return;
                    }

                    DALFactory.User.User_Admin.Info(_PageData.User_ID, function (retValue) {
                        if (retValue.hasError) {
                            callback(retValue);
                            return;
                        }
                        if (!retValue.returnObject) {
                            retValue.hasError = true;
                            retValue.message = "无此管理员";
                            callback(retValue);
                            return;
                        }
                        _PageData.User_ID = KOC.StringUtils.ToJSONStr(retValue.returnObject.User_ID);
                        _PageData.RealName = KOC.StringUtils.ToJSONStr(retValue.returnObject.RealName);
                        _PageData.Phone = KOC.StringUtils.ToJSONStr(retValue.returnObject.Phone);
                        _PageData.IsMaster = retValue.returnObject.IsMaster == 1;
                        //_PageData.IsKeeper = retValue.returnObject.IsKeeper == 1;
                        //_PageData.IsEngineer = retValue.returnObject.IsEngineer == 1;
                        //_PageData.Position = KOC.StringUtils.ToString(retValue.returnObject.Position);
                        //_PageData.Remark = KOC.StringUtils.ToString(retValue.returnObject.Remark);
                        //_PageData.Store_List = retValue.returnObject.Store_List;
                        _PageData.Status = retValue.returnObject.Status;
                        _RightCode = retValue.returnObject.RightCode;

                        callback(null);
                    });
                },
                //读取权限
                function (callback) {
                    KOC.Lodash.forEach(Config_Admin.MenuConfig, function (ThisValue_I) {
                        if (ThisValue_I.Setup === false) {
                            return true;
                        }
                        var _Item_II = [];
                        KOC.Lodash.forEach(ThisValue_I.Child, function (ThisValue_II) {
                            if (ThisValue_II.Setup === false) {
                                return true;
                            }
                            var _Item_III = [];
                            KOC.Lodash.forEach(ThisValue_II.Child, function (ThisValue_III) {
                                if (ThisValue_III.Setup === false) {
                                    return true;
                                }
                                _Item_III.push({
                                    Text: ThisValue_III.Text,
                                    Code: ThisValue_III.SetupCode || ThisValue_III.RightCode,
                                    Explain: ThisValue_III.Explain
                                });
                                if (_RightCode.indexOf(ThisValue_III.RightCode) >= 0) {
                                    _PageData.RightCode.Data.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                                }
                                _PageData.RightCode.All.push(ThisValue_III.SetupCode || ThisValue_III.RightCode);
                            });
                            _Item_II.push({
                                Text: ThisValue_II.Text,
                                Code: ThisValue_II.RightCode,
                                Explain: ThisValue_II.Explain,
                                Checked: _RightCode.indexOf(ThisValue_II.RightCode) >= 0,
                                List: _Item_III
                            });
                        });
                        _PageData.RightCode.TreeData.push({
                            Text: ThisValue_I.Text,
                            Code: ThisValue_I.RightCode,
                            Explain: ThisValue_I.Explain,
                            Checked: _RightCode.indexOf(ThisValue_I.RightCode) >= 0,
                            List: _Item_II
                        });
                    });
                    _PageData.RightCode.Default = _PageData.RightCode.Data;
                    callback(null);
                }
            ], function (retValue) {
                if (retValue) {
                    _PageData.flag = retValue.message;
                }
                CommonUtils.Common.PageScript_PageData(res, _PageData);
                res.render('system-manage/manager-manage/edit', callback);
            });
        },
        /////////////////////////////
        //日志列表
        ////////////////////////////
        Log_List: function (req, res, callback) {
            res.render("system-manage/manager-manage/log-list", callback);
        }
    }
};